package com.zz.hhbl.ui.earphone

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.content.Intent
import android.view.Gravity
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.zz.framework.core.base.BaseFragment
import com.zz.framework.core.ext.finish
import com.zz.framework.core.ext.gone
import com.zz.framework.core.ext.loadUrl
import com.zz.framework.core.ext.onClick
import com.zz.framework.core.ext.toLanguage
import com.zz.framework.core.ext.viewBinding
import com.zz.framework.core.toast.ToastUtils
import com.zz.framework.core.utils.DeviceUtils
import com.zz.framework.core.utils.language.Strings
import com.zz.framework.core.xdialog.XDialog
import com.zz.framework.core.xdialog.listener.OnBindViewListener
import com.zz.hhbl.R
import com.zz.hhbl.config.GlobalCache
import com.zz.hhbl.databinding.DialogWiredEarphoneConnectTipBinding
import com.zz.hhbl.databinding.FragmentEarphoneChoiceBinding
import com.zz.hhbl.ui.podcast.MainActivity

/**
 * Copyright © 2025 zhun All rights reserved.
 * Created by ZhaoZhun on 2025/2/26 17:41.
 * @author: ZhaoZhun 1820022519@qq.com
 * @version: V1.0
 */
class EarphoneChoiceFragment : BaseFragment(R.layout.fragment_earphone_choice) {
    val binding by viewBinding<FragmentEarphoneChoiceBinding>()
    private val REQUEST_ENABLE_BLUETOOTH = 9999
    private var bluetoothAdapter: BluetoothAdapter? = null
    override fun initView() {
        // 获取蓝牙适配器
        bluetoothAdapter = BluetoothAdapter.getDefaultAdapter()
        if (bluetoothAdapter == null) {
            ToastUtils.show(Strings.设备不支持蓝牙.toLanguage())
            return
        }
        binding?.apply {
            tvWiredHeadset.text = Strings.有线耳机.toLanguage()
            tvWirelessHeadset.text = Strings.蓝牙耳机.toLanguage()
            if (getIntent().getBooleanExtra("isBack", false)) {
                btnBack.text = Strings.返回.toLanguage()
            } else {
                btnBack.gone()
            }
            btnBack.onClick {
                finish()
            }
            showBackgroundImage()
            this.clWiredHeadset.onClick {
                //有线耳机
                showWiredHeadsetTipDialog()
            }
            this.clWirelessHeadset.onClick {
                //无线耳机
                XXPermissions.with(this@EarphoneChoiceFragment).permission(Permission.BLUETOOTH_CONNECT).request { permissions, allGranted ->
                    run {
                        if (allGranted) {
                            DeviceUtils.openBluetoothSettings(requireActivity(), REQUEST_ENABLE_BLUETOOTH)
                        }
                    }
                }
            }
            this.btnStart.setText(Strings.开始.toLanguage())
            this.btnStart.onClick {
                startActivity(MainActivity::class.java)
            }
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_ENABLE_BLUETOOTH) {
            if (isBluetoothConnected()) {
                startActivity(MainActivity::class.java)
            }
        }
    }

    @SuppressLint("MissingPermission")
    private fun isBluetoothConnected(): Boolean {
        // 检查是否有已连接的蓝牙设备
        return bluetoothAdapter?.bondedDevices?.size ?: 0 > 0
    }

    private fun showWiredHeadsetTipDialog() {
        XDialog.Builder(childFragmentManager)
            .setViewBinding(DialogWiredEarphoneConnectTipBinding::class.java) //设置弹窗展示的xml布局
            .setScreenWidthAspect(requireContext(), 1f)
            .setGravity(Gravity.BOTTOM) //设置弹窗展示位置
            .setTag("EarphoneConnectDialog")
            .setDimAmount(0.8f) //设置弹窗背景透明度(0-1f)
            .setCancelableOutside(true) //弹窗在界面外是否可以点击取消
            .setDialogAnimationRes(R.style.animate_dialog) //设置弹窗动画
            .setOnBindViewListener(object : OnBindViewListener<DialogWiredEarphoneConnectTipBinding> {
                override fun onBind(itemBinding: DialogWiredEarphoneConnectTipBinding, xDialog: XDialog) {
                    itemBinding?.apply {
                        tvTitle.text = Strings.连接您的有线耳机.toLanguage()
                        tvTitleTip.text = Strings.请在本机耳机接口处连接您的设备.toLanguage()
                        tvContent.text = Strings.若您未随身携带任何耳机设备可在李欢欢工作人员处购买.toLanguage()
                        btnConnect.text = Strings.知道了.toLanguage()
                        btnConnect.onClick {
                            startActivity(MainActivity::class.java)
                            xDialog.dismiss()
                        }
                    }
                }
            })
            .setOnKeyListener { dialog, keyCode, event ->
                true
            }
            .create() //创建TDialog
            .show() //展示
    }

    private fun showBackgroundImage() {
        GlobalCache.getSpotInfo()?.welcomeData?.firstOrNull()?.let {
            binding?.ivBackground?.loadUrl(it.url)
        }
    }

}